﻿Imports Cls_Common
Imports TemplateParser
Imports VSModel
Partial Class FrontEnd_Control_Users_ForgotPassword
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Page.Title = "Quên mật khẩu"
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Using Data As New VSEntities


            Dim p = (From q In Data.Users Where q.Email = txtEmail.Text Select q).FirstOrDefault
            If Not p Is Nothing Then
                If p.IsShowHome = True Then
                    Excute_Javascript("alert('Bạn đã yêu cầu reset mật khẩu trước đó, Vui lòng kiểm tra email để reset password');", Me.Page)
                    Exit Sub
                End If
                p.LastLogin = Now.Date
                'just for remeber user want to change pass.
                p.IsShowHome = True
                Data.SaveChanges()
                'send email with code to reset pass
                'send mail kích hoạt
                Dim templatevars As New Hashtable()
                templatevars.Add("FullName", p.FullName)
                templatevars.Add("Domain", MainDomain)

                templatevars.Add("Link", MainDomain & "reset-mat-khau.html?email=" & txtEmail.Text & "&code=" & p.Code & "")
                Dim parser As New Parser(Server.MapPath("~/EmailTemplate/ForGotPass.htm"), templatevars)

                'get tham so from parameter
                Dim SMTP_SERVER As String = (From q In Data.SYS_PARAMETERS Where q.Name = "SMTP_SERVER" Select q.Val).FirstOrDefault
                Dim SMTP_PORT As Integer = (From q In Data.SYS_PARAMETERS Where q.Name = "SMTP_PORT" Select q.Val).FirstOrDefault
                Dim ADMIN_EMAIL_ADDRESS As String = (From q In Data.SYS_PARAMETERS Where q.Name = "ADMIN_EMAIL_ADDRESS" Select q.Val).FirstOrDefault
                Dim ADMIN_EMAIL_PASSWORD As String = (From q In Data.SYS_PARAMETERS Where q.Name = "ADMIN_EMAIL_PASSWORD" Select q.Val).FirstOrDefault
                Dim SMTP_SSL As Boolean = (From q In Data.SYS_PARAMETERS Where q.Name = "SMTP_SSL" Select q.Val).FirstOrDefault
                Sendmail_SMTP(ADMIN_EMAIL_ADDRESS, "angiang", SMTP_SERVER, txtEmail.Text.Trim, "", "angiang - yeu cau reset mat khau", parser.Parse, ADMIN_EMAIL_PASSWORD, SMTP_PORT, SMTP_SSL)
                Excute_Javascript("AlertboxRedirect('Vui lòng kiểm tra email để cài đặt lại mật khẩu','" + MainDomain + "');", Me.Page)

            Else
                Excute_Javascript("Alertbox('Thông tin chưa chính xác vui lòng nhập lại');", Me.Page)

            End If
        End Using

    End Sub

End Class
